
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js"><!--<![endif]-->
<head>
  <meta charset="utf-8">
  <title>
    
  JavaScript一些有趣的地方 - beace lee的博客
  

  </title>
  <meta name="author" content="">
  <meta name="description" content="周日被我射熄火了，现在是周一">

  <meta name="HandheldFriendly" content="True">
  <meta name="MobileOptimized" content="320">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  
  <link href="asset/css/screen.css" media="screen, projection" rel="stylesheet" type="text/css">  
  <link href="atom.xml" rel="alternate" title="beace lee的博客" type="application/atom+xml">
  <script src="asset/js/modernizr-2.0.js"></script>
  <script src="asset/js/jquery.min.js"></script>
  <script src="asset/highlightjs/highlight.pack.js"></script>
  <link href="asset/highlightjs/styles/monokai_sublime.css" media="screen, projection" rel="stylesheet" type="text/css">
  <script>hljs.initHighlightingOnLoad();</script>

  <style type="text/css">
  .cat-children-p{ padding: 6px 0px;}
  .hljs{background: none;}
  </style>
  <script type="text/javascript">
  var isAddSildbar = true;
  </script>
  <script src="asset/js/octopress.js" type="text/javascript"></script>
</head>
<script type="text/javascript">
//链接新开窗口
function addBlankTargetForLinks () {
  $('a[href^="http"]').each(function(){
      $(this).attr('target', '_blank');
  });
}
$(document).ready(function(event) {
  addBlankTargetForLinks();
});
</script>
<body   >
  <header role="banner"><hgroup>
  <a href="index.html" class="beace-img">
    <img src="https://images-manager.oss-cn-shanghai.aliyuncs.com/self/logo.jpg" />
  </a>
  <div class="header-title">
    <h1><a href="index.html">beace lee的博客</a></h1>
    <h2>周日被我射熄火了，现在是周一
  </div>
    
</hgroup>

</header>
  <nav role="navigation"><ul class="subscription" data-subscription="rss">
  <li><a href="atom.xml" rel="subscribe-rss" title="subscribe via RSS">RSS</a></li>
  
</ul>
  
<!-- <form action="https://google.com/search" method="get">
  <fieldset role="search">
    <input type="hidden" name="q" value="site:beace.github.io" />
    <input class="search" type="text" name="q" results="0" placeholder="Search"/>
  </fieldset>
</form> -->
<a class="beace-github" href="https://github.com/BeAce"></a>
  
<ul class="main-navigation">

  <li id=""><a target="_self" href="index.html">Home</a></li>

  <li id=""><a target="_self" href="archives.html">Archives</a></li>

  <li id=""><a target="_self" href="about.html">About</a></li>

</ul>

</nav>
  <div id="main">
    <div id="content"> 
<div>
	<article class="hentry" role="article">
	<header>
			  	<h1 class="entry-title">JavaScript一些有趣的地方</h1>
				<p class="meta"><time datetime="2017-03-16T11:49:34+08:00" pubdate data-updated="true">2017/3/16 11:49 上午</time></p>
			 </header>
		  	<div class="entry-content">
			  	<h2 id="toc_0">Array-like</h2>

<p>所谓的Array-like,即类数组。其实并不是数组，但是可以像数组一样的进行访问，在这里，仅仅是写法上的一致。其实并不是数组。</p>

<h2 id="toc_1">对象(<em>Object</em>)</h2>

<ul>
<li>对象不加引号和加引号的区别，可以参考该链接（<a href="http://stackoverflow.com/questions/4348478/what-is-the-difference-between-object-keys-with-quotes-and-without-quotes%EF%BC%89">http://stackoverflow.com/questions/4348478/what-is-the-difference-between-object-keys-with-quotes-and-without-quotes）</a></li>
</ul>

<pre><code class="language-js">var a = {a: &quot;a&quot;};
var b = {&quot;b&quot;: &quot;b&quot;};
console.log(a); //&quot;a&quot;
console.log(b); //&quot;b&quot;
</code></pre>

<ul>
<li>特殊情况不可以用<em>点</em>的属性访问</li>
</ul>

<pre><code class="language-js">var a = {0: &quot;a&quot;}; 
console.log(a.0); //Uncaught SyntaxError: Unexpected number
//竟然可以像一个数组进行访问
console.log(a[0]); // &quot;a&quot;
//为了以便区分，可以加上引号
console.log(a[&#39;0&#39;]) // &quot;a&quot;
</code></pre>

<blockquote>
<p>No, the quotes do not make a difference (unless, as you noted, you want to use a key that’s not a valid JavaScript identifier).</p>
</blockquote>

<p>可见，如果符合规范的定义<code>key</code>值，加引号和不加引号是没有区别的。如果你的<code>key</code>值中出现数字或者<code>JavaScript</code>中的关键字，可以通过加引号的方式访问<code>obj[&#39;key&#39;]</code></p>

<h2 id="toc_2">引申</h2>

<p>说到这里，我想引申一个东西，跟上面的对象理解（类数组）完全一致。就是<em>JavaScript</em>函数中的<em>arguments</em>参数。</p>

<p><em>arguments</em>对象是所有<em>JavaScript</em>函数中的局部变量。通过<em>arguments</em>我们可以获取函数中的所有参数。</p>

<pre><code class="language-js">function getArgs(a, b) {
  console.log(arguments[0])
  console.log(arguments[1])  
}

getArgs(1, 2); // 1 2
</code></pre>

<p>通过上述代码，我们可以通过类似数组的方式进行参数的读取，判断等等。接下来的一篇文章将会探索<em>arguments</em>的真正类型以及含义。</p>

			</div>

		
	  
		<footer>
		 <p class="meta">

			<strong>Categories:</strong>&nbsp; 
			<span class="categories">
			
			    <a class='category' href='javascript-funny.html'>javascript-funny</a>&nbsp;
			 
			</span>
		    </p>
		    <p class="meta">
		      
		 </p>
	    
		<div class="sharing">
		  <!-- 多说公共JS代码 start (一个网页只需插入一次) -->
<script type="text/javascript">
var duoshuoQuery = {short_name:"shimin"};
  (function() {
    var ds = document.createElement('script');
    ds.type = 'text/javascript';ds.async = true;
    ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
    ds.charset = 'UTF-8';
    (document.getElementsByTagName('head')[0] 
     || document.getElementsByTagName('body')[0]).appendChild(ds);
  })();
  </script>
<!-- 多说公共JS代码 end -->
          

          
              <div class="ds-thread" data-thread-key="javascript-object-funny.html" data-url="beace.github.io/javascript-object-funny.html" data-title="JavaScript一些有趣的地方"></div>
          

		</div>

	    <p class="meta">
	    
	        <a class="basic-alignment left" href="JavaScript-arguments.html" 
	        title="Previous Post: JavaScript Arguments">&laquo; JavaScript Arguments</a>
	    
	    
	        <a class="basic-alignment right" href="css-draw-rectangle.html" 
	        title="Next Post: 分享一些CSS画图技巧(1)——矩形">分享一些CSS画图技巧(1)——矩形 &raquo;</a>
	    
	    </p>
	  </footer>
	</article>
</div>
 <aside class="sidebar"> 

	<section>
	  <h1>Categories</h1>
	  <ul id="recent_posts">
	  
	      <li class="post">
	        <a href="doc-css.html"><strong>doc-css&nbsp;(5)</strong></a>
	        
	        
	        
	      </li>
	  
	      <li class="post">
	        <a href="json-server.html"><strong>json-server&nbsp;(1)</strong></a>
	        
	        
	        
	      </li>
	  
	      <li class="post">
	        <a href="javascript-funny.html"><strong>javascript-funny&nbsp;(2)</strong></a>
	        
	        
	        
	      </li>
	  
	      <li class="post">
	        <a href="star.html"><strong>star&nbsp;(1)</strong></a>
	        
	        
	        
	      </li>
	  
	      <li class="post">
	        <a href="html5.html"><strong>html5&nbsp;(1)</strong></a>
	        
	        
	        
	      </li>
	   
	  </ul>
	</section>
	<section>
	  <h1>Recent Posts</h1>
	  <ul id="recent_posts">
	  
	      
		      <li class="post">
		        <a href="HTML5%20dataset.html">HTML5 dataset 属性不为人知的地方（命名规则和原理）</a>
		      </li>
	     
	  
	      
		      <li class="post">
		        <a href="14921760383479.html">【摘抄】You Don't Need jQuery (选自 https://travis-ci.org/oneuijs/You-Dont-Need-jQuery)</a>
		      </li>
	     
	  
	      
		      <li class="post">
		        <a href="JavaScript-arguments.html">JavaScript Arguments</a>
		      </li>
	     
	  
	      
		      <li class="post">
		        <a href="javascript-object-funny.html">JavaScript一些有趣的地方</a>
		      </li>
	     
	  
	      
		      <li class="post">
		        <a href="css-draw-rectangle.html">分享一些CSS画图技巧(1)——矩形</a>
		      </li>
	     
	  
	      
	  
	      
	  
	      
	  
	      
	  
	      
	   
	  </ul>
	</section>
	
</aside> </div></div>
  <footer role="contentinfo"><p>
  Copyright &copy; 2017 -  -
  <span class="credit">Powered by <a target="_blank" href="https://beace.github.io">Beace</a> &nbsp;&nbsp; Theme by <a href="http://octopress.org">Octopress</a></span>
</p>

</footer>
<script src="asset/js/big-picture.min.js"></script>  
<script>
  (function() {
    function setClickHandler(id, fn) {
      document.querySelector(id).onclick = fn;
    }
    setClickHandler('body', function(e) {
      e.target.tagName === 'IMG' && BigPicture({
        el: e.target,
        imgSrc: e.target.src
      });
    });
  })()
</script>

  


<script type="text/javascript">
var duoshuoQuery = {short_name:'shimin'};
  (function() {
    var ds = document.createElement('script');
    ds.type = 'text/javascript';ds.async = true;
    ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
    ds.charset = 'UTF-8';
    (document.getElementsByTagName('head')[0] 
     || document.getElementsByTagName('body')[0]).appendChild(ds);
  })();
  </script>
    



<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-83986231-2', 'auto');
  ga('send', 'pageview');

</script>
</body>
</html>